#include "paperball.h"

// 编写构造函数
PaperBall::PaperBall(const QString& text, QGraphicsItem* parent): QGraphicsItem(parent), content(text) {
    setFlag(QGraphicsItem::ItemIsMovable);

}

QRectF PaperBall::boundingRect() const{
    return QRectF(-100, -75, 200, 150);
}

void PaperBall::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) {
    painter->setRenderHint(QPainter::Antialiasing);

    // ==== 1. 绘制纸张主体（不规则矩形模拟褶皱） ====
    QPainterPath paperPath;
    QRectF baseRect(-100, -75, 200, 150);  // 基础矩形尺寸

    // 随机偏移四个顶点模拟边缘褶皱（偏移范围±3px）
    // 注意：QRandomGenerator::bounded 传入整数参数（避免qreal歧义）
    // 随机偏移四个顶点（偏移范围±10px，保持褶皱相对比例）
    QPointF topLeft = baseRect.topLeft() + QPointF(
        QRandomGenerator::global()->bounded(-10, 10),  // 整数区间，无歧义
        QRandomGenerator::global()->bounded(-10, 10)
    );
    QPointF topRight = baseRect.topRight() + QPointF(
        QRandomGenerator::global()->bounded(-10, 10),
        QRandomGenerator::global()->bounded(-10, 10)
    );
    QPointF bottomRight = baseRect.bottomRight() + QPointF(
        QRandomGenerator::global()->bounded(-10, 10),
        QRandomGenerator::global()->bounded(-10, 10)
    );
    QPointF bottomLeft = baseRect.bottomLeft() + QPointF(
        QRandomGenerator::global()->bounded(-10, 10),
        QRandomGenerator::global()->bounded(-10, 10)
    );

    paperPath.moveTo(topLeft);
    paperPath.lineTo(topRight);
    paperPath.lineTo(bottomRight);
    paperPath.lineTo(bottomLeft);
    paperPath.closeSubpath();

    // 渐变填充（米黄色→浅米黄，模拟纸张质感）
    QLinearGradient grad(baseRect.topLeft(), baseRect.bottomRight());
        grad.setColorAt(0, QColor(255, 245, 220));
        grad.setColorAt(1, QColor(255, 240, 200));
        painter->setBrush(grad);
        painter->setPen(QPen(QColor(200, 190, 160), 2)); // 浅灰色边框
        painter->drawPath(paperPath);

    // ==== 2. 绘制内部折痕线（模拟褶皱细节） ====
    painter->setPen(QPen(QColor(180, 170, 140), 1.5, Qt::DashLine));
    int foldCount = QRandomGenerator::global()->bounded(3, 6);

    // 获取矩形边界的具体数值（转换为qreal）
    qreal left = baseRect.left();
    qreal right = baseRect.right();
    qreal top = baseRect.top();
    qreal bottom = baseRect.bottom();

    for (int i = 0; i < foldCount; ++i) {
        // 生成 [left, right) 范围内的随机qreal（通过偏移量计算）
        qreal startX = left + QRandomGenerator::global()->bounded(right - left);
        qreal startY = top + QRandomGenerator::global()->bounded(bottom - top);
        qreal endX = left + QRandomGenerator::global()->bounded(right - left);
        qreal endY = top + QRandomGenerator::global()->bounded(bottom - top);

        painter->drawLine(QPointF(startX, startY), QPointF(endX, endY));
    }

        // ==== 3. 绘制情绪文字（适配矩形区域） ====
        painter->setFont(QFont("微软雅黑", 12, QFont::Medium));
        painter->setPen(Qt::black);
        painter->drawText(baseRect.adjusted(10, 10, -10, -10), // 文字区域内缩5px
        Qt::AlignLeft | Qt::TextWordWrap, // 左对齐+自动换行
        content.left(30)); // 最多显示40字（适应矩形宽度）
}

